package com.dfbz.controller.article;

import com.dfbz.controller.BaseServlet;
import com.dfbz.model.Article;
import com.dfbz.model.User;
import com.dfbz.service.impl.ArticleServletImp;
import com.dfbz.utils.PagingUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/article/*")
public class ArticleController extends BaseServlet {

    private ArticleServletImp articleServletImp = new ArticleServletImp();

    public void AllArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String titlesName = req.getParameter("titlesName");
        String articlePage = req.getParameter("ArticlePageNum");
        Integer articleCurrPage;
        if (null==titlesName){
            titlesName="";
        }
        if (articlePage==null){
            articleCurrPage=1;
        }else {
            articleCurrPage=Integer.parseInt(articlePage);
        }
        PagingUtil pagingUtil = new PagingUtil();
        pagingUtil.setCurrPage(articleCurrPage);
        Integer allArticleNum = articleServletImp.findAllArticleNum(titlesName);
        pagingUtil.setTotalCount(allArticleNum);
        Integer pageSize = pagingUtil.getPageSize();
        int i = (articleCurrPage - 1) * pageSize;
        List<Article> allArticle = articleServletImp.findAllArticle(titlesName, i, pageSize);
        pagingUtil.setData(allArticle);
        req.setAttribute("ArticleMessage",pagingUtil);
        req.getRequestDispatcher("/html/article.jsp").forward(req,resp);
    }

    public void articleDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String articleId = req.getParameter("articleId");
        String userId = req.getParameter("userId");
        int i1 = Integer.parseInt(userId);
        int i = Integer.parseInt(articleId);
        articleServletImp.addBrowse(i);
        Article byId = articleServletImp.getById(i);
        Integer focusNum = articleServletImp.getFocusNum(i1);
        Object user = req.getSession().getAttribute("user");
        User user1 = (User) user;
        Long id = user1.getId();
        List<Map<String, Object>> focusArticle = articleServletImp.findMyFocusUserFocusArticle(i1, id);
        req.setAttribute("focusArticle",focusArticle);
        req.setAttribute("byIdArticleMessage",byId);
        req.setAttribute("articleFocusNum",focusNum);
        req.getRequestDispatcher("/html/article_detail.jsp").forward(req,resp);
    }

    public void addArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user = req.getSession().getAttribute("user");
        User user1 = (User) user;
        Article article = new Article();
        Long id = user1.getId();
        String realName = user1.getRealName();
        article.setUserId(id);
        article.setPublishRealName(realName);
        Date date = new Date();
        article.setPublishDate(date);
        Long lo=0L;
        article.setBrowseCount(lo);
        String articleTitle = req.getParameter("articleTitle");
        article.setTitle(articleTitle);
        String articleCount = req.getParameter("articleCount");
        article.setContent(articleCount);
        articleServletImp.save(article);
        resp.sendRedirect(req.getContextPath()+"/article/AllArticle");
    }

    public void MyFocusArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user = req.getSession().getAttribute("user");
        String page = req.getParameter("page");
        Integer currPage;
        if (page==null){
            currPage=1;
        }else{
            currPage=Integer.parseInt(page);
        }
        User user1 = (User) user;
        Long id = user1.getId();
        Integer allMyFocusArticleNum = articleServletImp.getAllMyFocusArticleNum(id);
        PagingUtil pagingUtil = new PagingUtil();
        pagingUtil.setTotalCount(allMyFocusArticleNum);
        pagingUtil.setCurrPage(currPage);
        Integer pageSize = pagingUtil.getPageSize();
        int i = (currPage - 1) * pageSize;
        List<Map<String, Object>> myFocusArticle = articleServletImp.getMyFocusArticle(id, i, pageSize);
        pagingUtil.setData(myFocusArticle);
        req.setAttribute("myFocusArticle",pagingUtil);
        req.getRequestDispatcher("/html/article_collect.jsp").forward(req,resp);
    }
}
